<?php

if (!defined('UC_FILESYSTEM_ROOT')) 
{
    define('UC_FILESYSTEM_ROOT', dirname(__FILE__) . '/');
    require_once UC_FILESYSTEM_ROOT . 'UC_PHPFileSystemAutoloader.php';
}

class UC_PHPDirectory extends UC_PHPFileSystemElement
{
    public function __construct($path)
    {
        parent::__construct($path);
    }
    
    public function create_text_file($file_name, $overwrite = false)
    {
        
    }
    
    public function delete()
    {
        foreach($this->get_files() as $file)
        {
            $file->delete();
        }
        
        foreach($this->get_subdirectories() as $dir)
        {
            set_error_handler(array('UC_PHPDirectory', 'error_handler'));
            $dir->delete();
            restore_error_handler();
        }
        
        set_error_handler(array('UC_PHPDirectory', 'error_handler'));
        rmdir($this->get_path());
        restore_error_handler();
    }
    
    public function get_files()
    {
        $this->files = new UC_PHPFiles();
        
        foreach(scandir($this->get_path()) as $child_item)
        {
            if(is_file($this->get_path() . '/' . $child_item))
            {
                try
                {
                    $file = new UC_PHPFile($this->get_path() . '/' . $child_item);
                    $this->files->add($file);
                }
                catch(UC_PHPFileSystemException $e)
                {
                    // nothing
                }
            }
        }
        
        return $this->files;
    }

    public function get_subdirectories()
    {
        $this->subdirectories = new UC_PHPDirectories();
        
        foreach(scandir($this->get_path()) as $child_item)
        {
            if(is_dir($this->get_path() . '/' . $child_item) 
                    && $child_item != '.' && $child_item != '..')
            {
                try
                {
                    $dir = new UC_PHPDirectory($this->get_path() . '/' . $child_item);
                    $this->subdirectories->add($dir);
                }
                catch (UC_PHPFileSystemException $e)
                {
                    // nothing
                }
            }
        }
        
        return $this->subdirectories;
    }

    private static function error_handler($errno, $errstr, $errfile, $errline)
    {
        if(strpos($errstr, 'Permission denied'))
        {
            $trc = debug_backtrace();
            if(isset($trc[2]['function']))
            {
                if($trc[2]['function'] == 'delete')
                {
                    throw new UC_PHPFileSystemException(
                        'Unable to delete directory: permission denied' 
                        . ' in ' . $errfile . ' on line ' . $errline);
                }
            }
        }
        
        return false;
    }
    
    private $files;
    private $subdirectories;
}

?>
